package com.pr.makfilm.user.pages.room.create;

import com.pr.makfilm.core.cmp.authorization.UserAuthorities;
import com.pr.makfilm.domain.ObjectStateEnum;
import com.pr.makfilm.domain.Room;
import com.pr.makfilm.domain.RoomPlace;
import com.pr.makfilm.service.IRoomPlaceService;
import com.pr.makfilm.service.IRoomService;
import com.pr.makfilm.user.api.UserApiKeys;
import com.pr.makfilm.user.api.UserApiMappings;
import com.pr.makfilm.user.pages.AbstractUserPage;
import com.pr.makfilm.user.pages.room.list.RoomListPage;
import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.RestartResponseAtInterceptPageException;
import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.wicketstuff.annotation.mount.MountPath;

import java.util.List;

/**
 * User: pawel.radomski
 * Date: 09.12.13
 * Time: 21:00
 */
@MountPath(UserApiMappings.CREATION_FINISH_ROOM_PAGE)
@AuthorizeInstantiation(UserAuthorities.USER)
public class FinishRoomPage extends AbstractUserPage
{
   @SpringBean
   private IRoomService roomService;

   @SpringBean
   private IRoomPlaceService roomPlaceService;

   public FinishRoomPage(final PageParameters parameters)
   {
      super(parameters);
      Room room = getRoom(parameters);
      setDefaultModel(Model.of(room));
      List<RoomPlace> placeList = roomPlaceService.getByRoom(room);
//      placeTable = RoomPlaceUtils.convertToTable(placeList);
   }

   private Room getRoom(PageParameters parameters)
   {
      String sid = parameters.get(UserApiKeys.SID_KEY).toString();
      if (StringUtils.isNotBlank(sid))
      {
         Room obj = roomService.getBySid(sid);
         if (obj == null || obj.getObjectState() != ObjectStateEnum.INACTIVE)
         {
            throw new RestartResponseAtInterceptPageException(RoomListPage.class);
         }
         return obj;
      }
      else
      {
         throw new RestartResponseAtInterceptPageException(RoomListPage.class);
      }
   }
}
